package org.example.dbcompare.model;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * 导出SQL结果
 * @author wuyong
 * @date 2025/10/22
 */
@Data
public class ExportSqlResult {
    /**
     * 创建表的SQL语句列表
     */
    private List<String> createTableSqls = new ArrayList<>();
    
    /**
     * 添加列的SQL语句列表
     */
    private List<String> addColumnSqls = new ArrayList<>();
    
    /**
     * 修改列的SQL语句列表
     */
    private List<String> alterColumnSqls = new ArrayList<>();
    
    /**
     * 创建索引的SQL语句列表
     */
    private List<String> createIndexSqls = new ArrayList<>();
    
    /**
     * 所有SQL语句的汇总
     */
    private List<String> allSqls = new ArrayList<>();
    
    /**
     * 统计信息
     */
    private SqlStatistics statistics = new SqlStatistics();
    
    /**
     * 添加创建表SQL
     */
    public void addCreateTableSql(String sql) {
        if (sql != null && !sql.trim().isEmpty()) {
            createTableSqls.add(sql);
            allSqls.add(sql);
            statistics.incrementCreateTableCount();
        }
    }
    
    /**
     * 添加列SQL
     */
    public void addAddColumnSql(String sql) {
        if (sql != null && !sql.trim().isEmpty()) {
            addColumnSqls.add(sql);
            allSqls.add(sql);
            statistics.incrementAddColumnCount();
        }
    }
    
    /**
     * 添加修改列SQL
     */
    public void addAlterColumnSql(String sql) {
        if (sql != null && !sql.trim().isEmpty()) {
            alterColumnSqls.add(sql);
            allSqls.add(sql);
            statistics.incrementAlterColumnCount();
        }
    }
    
    /**
     * 添加创建索引SQL
     */
    public void addCreateIndexSql(String sql) {
        if (sql != null && !sql.trim().isEmpty()) {
            createIndexSqls.add(sql);
            allSqls.add(sql);
            statistics.incrementCreateIndexCount();
        }
    }
    
    /**
     * 获取完整的SQL脚本
     */
    public String getFullSqlScript() {
        StringBuilder script = new StringBuilder();
        script.append("-- 数据库结构同步SQL脚本\n");
        script.append("-- 生成时间: ").append(java.time.LocalDateTime.now()).append("\n\n");
        
        if (!createTableSqls.isEmpty()) {
            script.append("-- ============================================\n");
            script.append("-- 创建缺失的表\n");
            script.append("-- ============================================\n");
            for (String sql : createTableSqls) {
                script.append(sql).append("\n\n");
            }
        }
        
        if (!addColumnSqls.isEmpty()) {
            script.append("-- ============================================\n");
            script.append("-- 添加缺失的列\n");
            script.append("-- ============================================\n");
            for (String sql : addColumnSqls) {
                script.append(sql).append("\n\n");
            }
        }
        
        if (!alterColumnSqls.isEmpty()) {
            script.append("-- ============================================\n");
            script.append("-- 修改列结构\n");
            script.append("-- ============================================\n");
            for (String sql : alterColumnSqls) {
                script.append(sql).append("\n\n");
            }
        }
        
        if (!createIndexSqls.isEmpty()) {
            script.append("-- ============================================\n");
            script.append("-- 创建索引\n");
            script.append("-- ============================================\n");
            for (String sql : createIndexSqls) {
                script.append(sql).append("\n\n");
            }
        }
        
        script.append("-- ============================================\n");
        script.append("-- 统计信息\n");
        script.append("-- ============================================\n");
        script.append("-- 创建表: ").append(statistics.getCreateTableCount()).append(" 个\n");
        script.append("-- 添加列: ").append(statistics.getAddColumnCount()).append(" 个\n");
        script.append("-- 修改列: ").append(statistics.getAlterColumnCount()).append(" 个\n");
        script.append("-- 创建索引: ").append(statistics.getCreateIndexCount()).append(" 个\n");
        script.append("-- 总计SQL: ").append(allSqls.size()).append(" 条\n");
        
        return script.toString();
    }
    
    /**
     * SQL统计信息
     */
    @Data
    public static class SqlStatistics {
        private int createTableCount = 0;
        private int addColumnCount = 0;
        private int alterColumnCount = 0;
        private int createIndexCount = 0;
        
        public void incrementCreateTableCount() { createTableCount++; }
        public void incrementAddColumnCount() { addColumnCount++; }
        public void incrementAlterColumnCount() { alterColumnCount++; }
        public void incrementCreateIndexCount() { createIndexCount++; }
    }
}